//
//  KKNetworkResponse.m
//  Questionnaire
//
//  Created by leo on 16/1/24.
//  Copyright © 2016年 leo. All rights reserved.
//

#import "KKNetworkResponse.h" 

#import "NSObject+Function.h"

@implementation KKNetworkResponse

+ (instancetype)responseWithData:(NSData *)data request:(KKNetworkRequest *)request error:(NSString **)error {
    
    if (data != nil) {
        
        if (request.debug) {
            NSLog(@"responseString = %@", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
        }
        
        NSError *jsonError;
        id result = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &jsonError];
        
        if (result == nil && jsonError != nil) {
            *error = jsonError.description;
            return nil;
        }
        else {
            if (request.debug) {
                NSLog(@"response = %@", result);
            }
            
            if (![result isKindOfClass: [NSDictionary class]]) {
                *error = nil;
                return  nil;
            }
            
            NSDictionary *responseDictionary = (NSDictionary*)result;
            BOOL success = [responseDictionary[@"success"] boolValue];
            if (!success) {
                *error = responseDictionary[@"message"];
                return nil;
            }
            result = responseDictionary[@"data"];
            
            if ([result isKindOfClass: [NSDictionary class]]) {
                
                NSDictionary *resultDictionary = (NSDictionary*)result;
                if(request.responseClassName != nil){
                    Class class = NSClassFromString(request.responseClassName);
                    id object = [class new];
                    [object kk_parserJsonDictionary: resultDictionary];
                    KKNetworkResponse *response = [KKNetworkResponse new];
                    response->_responseObject = object;
                    return response;
                }
                else {
                    KKNetworkResponse *response = [KKNetworkResponse new];
                    response->_responseObject = resultDictionary;
                    return response;
                }
            }
            else if([result isKindOfClass: [NSArray class]]) {
                NSArray *resultArray = (NSArray*)result;
                
                if(request.responseClassName != nil){
                    
                    Class class = NSClassFromString(request.responseClassName);
                    NSMutableArray *itemArray = [NSMutableArray array];
                    for (NSDictionary *resultItem in resultArray) {
                        id object = [class new];
                        [object kk_parserJsonDictionary: resultItem];
                        [itemArray addObject: object];
                    }
                    KKNetworkResponse *response = [KKNetworkResponse new];
                    response->_responseObject = itemArray;
                    return response;
                }
                else {
                    KKNetworkResponse *response = [KKNetworkResponse new];
                    response->_responseObject = resultArray;
                    return response;
                }
            }
            else {
                KKNetworkResponse *response = [KKNetworkResponse new];
                response->_responseObject = result;
                return response;
            }
        }
    }
    *error = @"数据类型错误";
    return nil;
}

@end
